package com.bw.zg3park2212.carmanger

import android.annotation.SuppressLint
import android.app.ActionBar
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.TokenWatcher
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.view.Gravity
import android.view.LayoutInflater
import android.widget.Button
import android.widget.EditText
import android.widget.PopupWindow
import android.widget.TextView
import androidx.appcompat.widget.SearchView
import androidx.core.widget.addTextChangedListener
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.bw.base.db.SearchEntity
import com.bw.base.entity.CarmangerAddParam
import com.bw.base.mvvm.BaseActivity
import com.bw.zg3park2212.R
import com.bw.zg3park2212.adpter.CarMangerAdpter
import com.bw.zg3park2212.databinding.ActivityCarMangnerBinding
import com.google.android.material.chip.Chip
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = "/app/CarMangnerActivity")
class CarMangnerActivity : BaseActivity<ActivityCarMangnerBinding,CarMangerViewModel>() {
    var cont=""

    lateinit var carMangerAdpter: CarMangerAdpter
    @SuppressLint("MissingInflatedId")
    override fun intitView() {
        binding.sv.addTextChangedListener ( object :TextWatcher{
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

            }

            override fun afterTextChanged(s: Editable?) {
                if (s.toString().length==3||s.toString().length==8){
                    cont=s.toString()
                    viewmodel.addserch(SearchEntity(0,s.toString()))
                    queryall()
                }
            }
        } )

//        binding.sv.setOnQueryTextListener(object :SearchView.OnQueryTextListener{
//            override fun onQueryTextSubmit(query: String?): Boolean {
//                if (query!=null){
//                    cont=query
//                    viewmodel.addserch(SearchEntity(0,query))
//                    queryall()
//                }
//                return true
//            }
//
//            override fun onQueryTextChange(newText: String?): Boolean {
//                cont=""
//                binding.rvClgl.removeAllViews()
//                carMangerAdpter.data.clear()
//                carMangerAdpter.notifyDataSetChanged()
//                return false
//            }
//        })
        queryall()
        carMangerAdpter= CarMangerAdpter()
        binding.rvClgl.adapter=carMangerAdpter
        binding.rvClgl.layoutManager=LinearLayoutManager(this)
        binding.btnCarid.setOnClickListener {
            if (cont.isEmpty()){
                ToastUtils.showLong("请输入查询内容")
                return@setOnClickListener
            }
            viewmodel.carid(cont.toInt())
        }
        binding.btnCarcode.setOnClickListener {
            if (cont.isEmpty()){
                ToastUtils.showLong("请输入查询内容")
                return@setOnClickListener
            }
            viewmodel.carcode(cont)
        }
        binding.addCar.setOnClickListener {
            ARouter.getInstance().build("/app/CarMangnerAddActivity").navigation()
        }
        carMangerAdpter.addChildClickViewIds(R.id.car_del,R.id.car_up)
        carMangerAdpter.setOnItemChildClickListener { adapter, view, position ->
            when(view.id){
                R.id.car_del->{
                    viewmodel.cardel(carMangerAdpter.data[position].id)
                }
                R.id.car_up->{
                    val popupWindow= PopupWindow(this)
                    val inflate = LayoutInflater.from(this).inflate(R.layout.item_popcarmangerup, null)
                    val btnYes = inflate.findViewById<Button>(R.id.btn_yes)
                    val btnNo = inflate.findViewById<Button>(R.id.btn_no)
                    val carman_licensePlate = inflate.findViewById<EditText>(R.id.carman_licensePlate)
                    val carman_ownerName = inflate.findViewById<EditText>(R.id.carman_ownerName)
                    val carman_provinceAbbreviation = inflate.findViewById<EditText>(R.id.carman_provinceAbbreviation)
                    btnYes.setOnClickListener {
                        val carmangerAddParam=CarmangerAddParam()
                        carmangerAddParam.licensePlate=carman_licensePlate.text.toString()
                        carmangerAddParam.ownerName=carman_ownerName.text.toString()
                        carmangerAddParam.provinceAbbreviation=carman_provinceAbbreviation.text.toString()
                        viewmodel.carmanup(cont.toInt(),carmangerAddParam)
                    }
                    btnNo.setOnClickListener {
                        popupWindow.dismiss()
                    }
                    popupWindow.isFocusable=true
                    popupWindow.isOutsideTouchable=true
                    popupWindow.width= ActionBar.LayoutParams.MATCH_PARENT
                    popupWindow.height=500
                    popupWindow.contentView=inflate
                    val car_up = carMangerAdpter.getViewByPosition(position, R.id.car_up)
                    popupWindow.showAtLocation(car_up, Gravity.BOTTOM,0,0)
                }
            }
        }
    }

    private fun queryall() {
        viewmodel.queryserch()
        viewmodel.querysearchSuccse.observe(this){
            if (it.isNotEmpty()){
                binding.rvSearch.removeAllViews()
                it.forEach {
                    val chip = LayoutInflater.from(this).inflate(R.layout.item_search, null) as Chip
                    chip.findViewById<TextView>(R.id.chip).text=it.name
                    binding.rvSearch.addView(chip)
                    chip.setOnClickListener { it1->
                        binding.sv.setText(it.name)
                    }
                    chip.setOnCloseIconClickListener { it2->
                        binding.rvSearch.removeView(it2)
                        viewmodel.delsearch(it)
                    }
                }
            }
        }


    }

    override fun intitData() {
        viewmodel.caridSuccse.observe(this){
            ToastUtils.showLong("根据id查询成功")
            carMangerAdpter.data.clear()
            carMangerAdpter.addData(it)
            carMangerAdpter.notifyDataSetChanged()
        }
        viewmodel.carcodeSuccse.observe(this){
            ToastUtils.showLong("根据车牌查询成功")
            carMangerAdpter.data.clear()
            carMangerAdpter.addData(it)
            carMangerAdpter.notifyDataSetChanged()
        }
        viewmodel.carDelSuccse.observe(this){
            ToastUtils.showLong("删除成功")
            carMangerAdpter.data.clear()
        }
        viewmodel.carmanupSuccse.observe(this){
            ToastUtils.showLong("修改成功")
            carMangerAdpter.data.clear()

        }
        viewmodel.addsearchSuccse.observe(this){
            ToastUtils.showLong("添加成功")
        }
    }


}